interface Props {
	data: any;
	getDetail: any;
}

import { useState, useMemo, useEffect } from "react";
import styles from "./index.module.less";
import JudgeStatus from "../../JudgeStatus";
import { STEP_ITEMS_SALES, STEP_ITEMS_closed, STEP_ITEMS_quick_refund_success, STEP_ITEMS_refund } from "./constant";
import { Button, Space, Steps } from "antd";
import useUrl from "@/hooks/useUrl";
import useRefundOperation from "../../useRefundOperation";
import { isEmpty } from "lodash";

export default (props: Props) => {
	const { data, getDetail } = props;
	const { navigate, search } = useUrl();

	const [stepInfo, setStepInfo] = useState({
		items: [],
		current: 0
	});
	const { onAgree, onRefuse } = useRefundOperation({
		successCallback: getDetail
	});

	//是否是待发货
	const isWaitSales = JudgeStatus.isWaitSales(data);
	//是否是已发货
	const isSales = JudgeStatus.isSales(data);
	//是否是交易成功的订单
	const isSalesSuccess = JudgeStatus.isSalesSuccess(data);
	//是否是退款中
	const isRefund = JudgeStatus.isInRefunded(data);
	//是否是交易成功待评价
	const isSalesSuccessWaitComment = JudgeStatus.isSalesSuccessWaitComment(data);
	//是否交易成功已评价
	const isSalesSuccessCommented = JudgeStatus.isSalesSuccessCommented(data);
	//是否是交易关闭的订单（-1超时交易关闭的）
	const isSalesClose = JudgeStatus.isSalesClose(data);
	//是否退款成功
	const isRefundSuccess = JudgeStatus.isInRefundedComplete(data);
	//是否是极速退款
	const isQuickRefund = JudgeStatus.isQuickRefund(data);
	//是否是退款待商家审核
	const isRefundMerchantAudit = JudgeStatus.isInRefundedWaitAudit(data);
	//是否是商家拒绝退款
	const isRefundRefuse = JudgeStatus.isMerchantInRefundedRefuse(data);
	//是否是部分发货
	const isPartDeliver = JudgeStatus.isPartialDelivery(data);

	useEffect(() => {
		let initInfo: any = {
			items: [],
			current: 0
		};
		//待发货-已发货-交易成功
		if (isWaitSales || isSales || isSalesSuccess) {
			initInfo = {
				items: STEP_ITEMS_SALES,
				current: isWaitSales ? 2 : isSales ? 3 : isSalesSuccessWaitComment ? 4 : 5
			};
			//退款
		} else if (isRefund) {
			if (isQuickRefund) {
				initInfo = {
					items: STEP_ITEMS_quick_refund_success,
					current: 3
				};
			} else {
				initInfo = {
					items: STEP_ITEMS_refund,
					current: data?.aftersale_status_info?.order_aftersales_status_id === 3 ? 2 : 1
				};
			}
			// if (isRefundSuccess) {
			// 	if (isQuickRefund) {
			// 		initInfo = {
			// 			items: STEP_ITEMS_quick_refund_success,
			// 			current: 3
			// 		};
			// 	} else {
			// 		initInfo = {
			// 			items: STEP_ITEMS_refund,
			// 			current: 1
			// 		};
			// 	}
			// } else {
			// 	initInfo = {
			// 		items: STEP_ITEMS_refund,
			// 		current: 1
			// 	};
			// }
		} else if (isSalesClose) {
			initInfo = {
				items: STEP_ITEMS_closed,
				current: 1
			};
		}
		setStepInfo(initInfo);
	}, [data]);

	//去发货
	const handleGoDeliverGoods = () => {
		navigate(`/transaction/deliver${search}`);
	};

	//拒绝退款申请
	const handleRefuse = () => {
		onRefuse(data);
	};
	//同意退款申请
	const handleAgree = () => {
		onAgree(data);
	};

	// console.log("======stepInfo:", stepInfo);

	return (
		<div className={styles.container}>
			<Steps className={styles.steps} size="small" current={stepInfo.current} items={stepInfo.items} />
			{/* 待发货 */}
			{(isWaitSales || isPartDeliver) && (
				<>
					<div className={styles.title}>订单状态：买家已付款，等待商家发货</div>
					<Button className={styles.btn} type="primary" onClick={handleGoDeliverGoods}>
						{isPartDeliver ? "继续发货" : "去发货"}
					</Button>
				</>
			)}
			{/* 已发货 */}
			{isSales && !isPartDeliver && (
				<>
					<div className={styles.title}>订单状态：商家已发货，等待买家确认</div>
					<div className={styles.msg}>
						商品已于：<span className={styles.red}>{data?.logisticsInfo?.[0]?.created_at ?? "-"}</span> 完成发货，等待买家收货中。
						{/* 买家还有<span className={styles.red}>9天23小时55分45</span>
						秒来完成本次交易的"确认到货"。如果期间买家没有"确认到货"，也没有"申请退款"，本交易将自动结束。 */}
					</div>
				</>
			)}
			{/* 待评价 */}
			{(isSalesSuccessWaitComment || isSalesSuccessCommented) && (
				<>
					<div className={styles.title}>{isSalesSuccessWaitComment ? "订单状态：交易成功，待评价" : "订单状态：交易成功"}</div>
					<div className={styles.msg}>{isSalesSuccessWaitComment ? "订单交易成功" : "卖家已评价"}</div>
				</>
			)}
			{/* 交易超时关闭订单 */}
			{isSalesClose && (
				<>
					<div className={styles.title}>订单状态： 买家未付款 - 交易关闭</div>
					<div className={styles.msg}>交易超时自动关闭</div>
				</>
			)}
			{/* 急速退款----退款成功 */}
			{isRefundSuccess && (
				<>
					<div className={styles.title}>
						订单状态：
						{isQuickRefund
							? "未发货 - 退款成功"
							: `${isEmpty(data?.order_have_send_spec_ids) ? "未发货 - " : "已发货 - "}${
									data?.aftersale_status_info?.order_aftersales_status_name
							  }`}
					</div>
					<div className={styles.msg}>退回微信支付</div>
				</>
			)}
			{/* 退款卖家审核 */}
			{isRefundMerchantAudit && (
				<>
					<div className={styles.title}>
						订单状态：买家申请退款，<span className={styles.red}>卖家审核</span>
					</div>
					<Space className={styles.btns} size={20}>
						<Button type="primary" ghost onClick={handleRefuse}>
							拒绝申请
						</Button>
						<Button type="primary" onClick={handleAgree}>
							同意申请
						</Button>
					</Space>
				</>
			)}
			{/* 买家拒绝退款 */}
			{isRefundRefuse && (
				<div className={styles.title}>
					订单状态：买家申请退款，<span className={styles.red}>卖家已拒绝退款申请</span>
				</div>
			)}
		</div>
	);
};
